package com.blast.comicsisland.adapters;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.blast.comicsisland.activity.MainActivity;

import java.util.List;

/**
 * Created by Android Studio
 * Author: hg
 * Date: 15/6/24
 * Email: 531057892@qq.com
 */
public class GuideAdapter extends PagerAdapter implements View.OnClickListener {

    private Context context;
    private List<Integer> imageIds;
    private Handler handler;

    public GuideAdapter(Context context, List<Integer> imageIds,Handler handler) {
        this.context = context;
        this.imageIds = imageIds;
        this.handler = handler;
    }

    @Override
    public int getCount() {
        int ret = 0;
        if (imageIds != null) {
            ret = imageIds.size();
        }
        return ret;
    }

    @Override
    public boolean isViewFromObject(View view, Object o) {
        return view == o;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View ret = null;
        if (imageIds != null) {
            ImageView imageView = new ImageView(context);
            Integer id = imageIds.get(position);
            imageView.setImageResource(id);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

            if (position == 3) {
                imageView.setTag(position);
                imageView.setOnClickListener(this);
            }
            ret = imageView;
            container.addView(imageView);

        }
        return ret;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        if (object instanceof View) {
            container.removeView((View) object);
        }
    }

    @Override
    public void onClick(View v) {
        int position = (int) v.getTag();
        if (position == 3){
            if (v instanceof ImageView){
                Message msg = new Message();
                msg.what = 998;
                handler.sendMessage(msg);
            }
        }
    }
}
